<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/28
 * Time: 16:35
 */
namespace think;
use app\data\Data;

/**
 * Class Helpers
 * @package app\helpers
 * 助手类，主要用来处理数组和字符串
 */
class Helper{
    /**
     * @param array $tag_ids  标签的ID编号
     * @param int $article_id  文章ID编号
     * @return array|bool    参数错误返回false,成功返回处理之后的数组
     */
    public static function articleTag($tag_ids = [],$article_id = 0){
        if(empty($tag_ids) || $article_id == 0){
            return [];
        }
        foreach($tag_ids as $k=>$v){
            $article_tag[] = ['article_id'=>$article_id,'tag_id'=>$tag_ids[$k]];
        }
        return $article_tag;
    }

    /**
     * @param $str
     * @return string
     * 过滤掉html标签
     */
    public static function articleStripTags($str){
        return mb_substr(strip_tags((trim(html_entity_decode($str),'&nbsp;'))),0,180,'utf-8');
    }
    /**
     * @param array $tag_ids
     * @return string
     *
     */
    public static function implodeTag($tag_ids = []){
        $str_ids = '';
        foreach($tag_ids as $v){
            $str_ids .= $v['tag_id'].',';
        }
        $arr_ids = explode(',',trim($str_ids,','));
        return $arr_ids;
    }

    /**
     * @param array $parent_button
     * @param array $son_button
     * @return mixed
     * 主要处理后台按钮
     */
    public static function button($parent_button = [],$son_button = []){
        foreach($parent_button as $k=>$v){
            foreach($son_button as $k1=>$v1){
                if($v1['p_id'] == $v['id']){
                    $button[$k]['parentButton'] = $v;
                    $button[$k]['sonButton'][]  = $v1;
                }
            }
        }
        return $button;
    }

    /**
     * @param array $data
     * 读取配置文件
     */
    public static function  config($data = []){
        foreach($data as $v){
            $config[$v['cname']] = $v['option'];
        }
        return $config;
    }

    /**
     * @param array $tag_list
     * @return array
     * 统计对应栏目的数据
     */
    public static function countTag($tag_list = []){
        $article_tag = new Data('article_tag');
        foreach($tag_list as $k=>$v){
            $tag_list[$k]['count'] = $article_tag->count("tag_id = {$v['id']}");
        }
        return $tag_list;
    }

    /**
     * @param array $tag_id
     * @param int $article_id
     * @return string
     * 处理相关文章条件
     */
    public static function relatedWhere($tag_id = [],$article_id = 0){
        $where = "(a.id > 0";
        foreach($tag_id as $v){
            $where .= " or a.tag_id = {$v['tag_id']}";
        }
        $where .= ')  and b.id != '.$article_id;
        return $where;
    }
}